package com.lw.nio.channel.socketChannel;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author LW
 * @2021/9/12 19:37
 */
public class SocketChannelDemo {
    public static void main(String[] args) throws Exception {
        //创建SocketChannel
        SocketChannel sc = SocketChannel.open(new InetSocketAddress("www.baidu.com", 80));

        /**连接校验
        sc.isOpen();//是否为开启状态
        sc.isConnected();//是否已经被连接
        sc.isConnectionPending();//测试是否正在进行连接
        sc.finishConnect();//校验正在进行套接字连接的SocketChannel是否以及完成连接
         */

        //设置非阻塞模式
        sc.configureBlocking(false);

        //读操作
        ByteBuffer buffer = ByteBuffer.allocate(16);
        sc.read(buffer);
        sc.close();
        System.out.println("读操作结束");
    }

}
